package com.sunxiansheng.user.event;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * Description: 监听事件
 * @Author sun
 * @Create 2024/7/24 14:35
 * @Version 1.0
 */
@Service
@Slf4j
public class PersonEventListener {

    /**
     * 监听事件的方法
     * @param personChangeEvent
     */
    @Async("taskExecutor")
    @TransactionalEventListener(fallbackExecution = true)
    public void listenEvent(PersonChangeEvent personChangeEvent) {
        switch (personChangeEvent.getOperateType()) {
            case "create":
                // 业务逻辑
                log.info("执行Person创建事件：{}，处理的线程：{}", JSON.toJSONString(personChangeEvent.getPerson()),
                        Thread.currentThread().getName());
                break;
            default:
                break;
        }
    }

}